import * as winston from 'winston';
import {
  utilities as nestWinstonModuleUtilities,
  WinstonModuleOptions,
} from 'nest-winston';
import 'winston-daily-rotate-file';
import { ConfigService } from '@nestjs/config';
import chalk from 'chalk';
export const winstonConfig = (
  configService: ConfigService,
): WinstonModuleOptions => {
  const consoleFormat = winston.format.combine(
    winston.format.timestamp({
      format: `YYYY/MM/DD HH:mm:ss`,
    }),
    winston.format.ms(),
    nestWinstonModuleUtilities.format.nestLike('Nest', {
      prettyPrint: false,
      processId: true,
    }),
    winston.format.printf((info) => {
      return `${chalk.green(`[Nest]`)} ${chalk.green(process.pid)} ${chalk.green(' -')} ${info.timestamp}     ${chalk.green('LOG')} ${chalk.yellow(`${info.context}`)} ${chalk.green(info.message)} ${chalk.yellow(info.ms)}`;
    }),
  );
  const fileFormat = winston.format.combine(
    winston.format.timestamp({ format: 'YYYY/MM/DD HH:mm:ss' }),
    winston.format.uncolorize(),
    winston.format.json(),
  );
  return {
    level: configService.get('LOG-LEVEL') || 'info',
    transports: [
      new winston.transports.Console({
        format: consoleFormat,
      }),
      new winston.transports.DailyRotateFile({
        level: 'error',
        filename: 'logs/error-%DATE%.log',
        datePattern: 'YYYY-MM-DD',
        zippedArchive: true,
        maxSize: '20m',
        maxFiles: '14d',
        format: fileFormat,
      }),
      new winston.transports.DailyRotateFile({
        filename: 'logs/combined-%DATE%.log',
        datePattern: 'YYYY-MM-DD',
        zippedArchive: true,
        maxSize: '20m',
        maxFiles: '30d',
        format: fileFormat,
      }),
    ],
  };
};
